package com.example.zuoye;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private static final int REQUEST_OF_REGISTER=1;
    String username;
    String password;
    String gender;
    String age;

    private TextView tvUsername;
    private EditText etUsername;
    private TextView tvPassword;
    private EditText etPassword;
    private Button btnRegu;
    private Button btnLogin;

   String username_input;
   String password_input;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        tvUsername = (TextView) findViewById(R.id.tv_username);
        etUsername = (EditText) findViewById(R.id.et_username);
        tvPassword = (TextView) findViewById(R.id.tv_password);
        etPassword = (EditText) findViewById(R.id.et_password);
        btnRegu = (Button) findViewById(R.id.btn_regu);
        btnLogin = (Button) findViewById(R.id.btn_login);
    }

    public void login(View view) {
        username_input=etUsername.getText().toString();
        password_input=etPassword.getText().toString();
        if(username!=null&&password!=null){
            if(username.equals(username_input)&&password.equals(password_input)){
            Intent intent =new Intent(this,MainActivity.class);
            intent.putExtra("username",username);
            intent.putExtra("password",password);
            intent.putExtra("gender",gender);
            intent.putExtra("age",age);
            startActivity(intent);
            }
           else{ Toast.makeText(this,"用户名或密码错误",Toast.LENGTH_SHORT).show();}
        }else {
            Toast.makeText(this,"请先注册",Toast.LENGTH_SHORT).show();
        }

    }

    public void register(View view) {
        Intent intent=new Intent(this,RegisterActivity.class);
        startActivityForResult(intent,REQUEST_OF_REGISTER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable  Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQUEST_OF_REGISTER){

            if(resultCode== Activity.RESULT_OK){
                username=data.getStringExtra("username");
                password=data.getStringExtra("password");
                gender=data.getStringExtra("gender");
                age=data.getStringExtra("age");
                etUsername.setText(username);
                etPassword.setText(password);
            }
        }
    }
}